"""

主启动函数🚀

"""

# 1. 导入依赖模块部分
import sys
import os

from Agent.Dispatch.Service.DispatchService import DispatchService
from Agent.SecondBarging.Controller.SecondBargingController import SecondBargingController
from config import settings

# 添加项目根目录到Python路径，确保能正确导入其他模块
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# 导入FastAPI框架相关
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

# 导入日志工具
from utils.logger import get_logger

# 初始化日志记录器
logger = get_logger(__name__)

# 🌈 2. 创建FastAPI应用实例
app = FastAPI(
    title=settings.PROJECT_NAME,  # 从配置中读取项目名称
    version=settings.PROJECT_VERSION,  # 从配置中读取项目版本
    debug=settings.DEBUG  # 从配置中读取调试模式
)

# 🔄 3. 添加中间件配置
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源(开发环境使用)
    allow_credentials=True,  # 允许携带凭证
    allow_methods=["*"],  # 允许所有HTTP方法
    allow_headers=["*"],  # 允许所有请求头
)

# 🤖 4. 初始化并注册智能体
# 实例化各个智能体
dispatch_agent = DispatchService()
# fill_agent = FillAgent()
# save_agent = SaveAgent()
# status_agent = StatusAgent()
# conversation_processor_agent = ConversationProcessorAgent()
# rebate_identification_agent = RebateIdentificationAgent()


# 将智能体注册到注册中心
# registry.register("dispatch_agent", DispatchAgent)
# registry.register("fill_agent", FillAgent)
# registry.register("save_agent", SaveAgent)
# registry.register("status_agent", StatusAgent)
# registry.register("conversation_processor_agent", ConversationProcessorAgent)
# registry.register("rebate_identification_agent", RebateIdentificationAgent)


# 🛣️ 5. 添加路由配置
# 将各个智能体的路由添加到主应用
# app.include_router(dispatch_agent.router)
# app.include_router(fill_agent.router)
# app.include_router(save_agent.router)
# app.include_router(status_agent.router)
# app.include_router(conversation_processor_agent.router)
# app.include_router(rebate_identification_agent.router)

# 添加统一服务和二次议价服务的路由
# app.include_router(unified_service.router)
app.include_router(SecondBargingController, tags=["二次议价服务的路由"])


# 🏠 6. 定义根路由
@app.get("/")
async def root():
    return {
        "message": "Welcome to Agents System",
        "version": settings.PROJECT_VERSION,
        # "agents": registry.list_agents()  # 显示已注册的智能体列表
    }


# 💓 7. 健康检查接口
@app.get("/health")
async def health_check():
    return {"status": "healthy"}  # 简单的健康状态返回


# 🚦 8. 启动应用
if __name__ == "__main__":
    logger.info("Starting Agents System")  # 记录启动日志
    # 使用Uvicorn运行FastAPI应用
    uvicorn.run("main:app", host="0.0.0.0", port=8000)
